home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cdtray1a / csystray.cls < prev    next >
Text File  |  1999-09-29  |  9KB  |  233 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CSystrayIcon"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'Win32 API declaration
  17. 'GetLastError return the code number of the error when the systray access fail
  18. Private Declare Function GetLastError Lib "kernel32" () As Long
  19. 'Shell_NotifyIcon is THE function to add, modify or delete an existing icon.
  20. 'If it return 'True', that mean that the call was successful.
  21. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
  22. 'Arguments of 'Shell_NotifyIcon':
  23.                   'dwMessage: double word message. This represent the action
  24.                   'to execute on the Systray. The message can be one of
  25.                   'the following constants:
  26. Private Const NIM_ADD = &H0 'Add a new icon to the Systray
  27. Private Const NIM_MODIFY = &H1 'Modify an existing icon
  28. Private Const NIM_DELETE = &H2 'Delete an existing icon
  29.                   
  30.                   'pnid: pointer to a NOTIFYICONDATA type (see below)
  31.                   'This represent the info on the icon.
  32.  
  33. 'Constants for the member 'uFlag' of the NOTIFYICONDATA type
  34. 'uFlag can be a combinaison of these three constants
  35. Private Const NIF_MESSAGE = &H1 'Tell that the message has been updated
  36. Private Const NIF_ICON = &H2 'Tell that the icon picture has been changed
  37. Private Const NIF_TIP = &H4 'Tell that a new ToopTip for the icon is set
  38.  
  39. Private Const WM_MOUSEMOVE = &H200 'Used as the ID of the callback message
  40.  
  41. Private Const MAX_TIP_LENGTH As Long = 64 'This is the max length
  42.                   'of a ToolTip. This value has been tested for Win95.
  43.                   'For Win98 and NT, try changing this value. Tell me
  44.                   'if it is a different value.
  45.  
  46. Private Type NOTIFYICONDATA
  47.     cbSize As Long 'The size of this type
  48.     hWnd As Long 'The hWnd that will receive the CallBack message
  49.     uId As Long 'The ID of the application. Zero represent this application
  50.     uFlags As Long 'The flags. Look at the constants beginning by NIF_ for the flags and their definition
  51.     uCallbackMessage As Long 'This is the callback message
  52.     hIcon As Long 'An handle to the icon that will be displayed
  53.     szTip As String * MAX_TIP_LENGTH 'The string of the ToopTip. Must be terminating by a null zero (chr(0))
  54. End Type
  55.  
  56. 'The variable that will be used to interfere with the Systray
  57. Private nidTrayIcon As NOTIFYICONDATA
  58.  
  59. Private bIconDisplayed As Boolean 'The status of the icon. True=Displayed
  60. Private bUpdateOnChange As Boolean 'If True, Shell_NotifyIcon is call
  61.                                   'whenever a change to a properties
  62.                                   'of nidTrayIcon is made
  63.                                   'True when class is initialized.
  64.  
  65. Public Event NIError(ByVal ErrorNumber As Long)
  66.  
  67. Public PopUpMessage As String
  68.  
  69.  
  70.  
  71.  
  72. 'Now for the Class Members
  73. Public Function Initialize(ByVal hWnd As Long, ByVal hIcon As Long, ByVal sTip As String, Optional ByVal uCallbackMessage As Long = WM_MOUSEMOVE) As Long
  74.   'Initialize the systray icon (The icon won't show)
  75.   'THIS FUNCTION MUST BE THE FIRST MEMBER TO BE CALL AFTER
  76.   'CREATING A INSTANCE OF THIS CLASS
  77.   '
  78.   'Input:   hWnd: Handle of the window that receives notification
  79.   '               messages associated with an icon in the taskbar
  80.   '               status area.
  81.   '         hIcon: Handle of the icon to add, modify, or delete.
  82.   '                This is not directly the bitmap. It is his handle
  83.   '                in memory. So instead of using Picture1.Picture,
  84.   '                you can use LoadPicture() or LoadResPicture() if
  85.   '                you don't want to use a Picturebox.
  86.   '
  87.   '         szTip: Tooltip text to display for the icon.
  88.   '                Max length: MAX_TIP_LENGTH - 1
  89.   '                The class will add the null zero needed.
  90.   '                The class will truncate the string if too long.
  91.   '
  92.   '         uCallbackMessage: Application-defined message
  93.   '               identifier. The system uses the specified
  94.   '               identifier for notification messages that it
  95.   '               sends to the window identified by hWnd
  96.   '               whenever a mouse event occurs in the bounding
  97.   '               rectangle of the icon.
  98.   '               Default: &H200 (512, WM_MOUSEMOVE)
  99.   With nidTrayIcon
  100.    .cbSize = Len(nidTrayIcon)
  101.    .hIcon = hIcon
  102.    .hWnd = hWnd
  103.    .szTip = Left(sTip, MAX_TIP_LENGTH - 1) & vbNullChar
  104.    .uCallbackMessage = uCallbackMessage
  105.    .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  106.    .uId = vbNull
  107.   End With
  108.   bIconDisplayed = False
  109.   bUpdateOnChange = True
  110. End Function
  111.  
  112. Public Function ShowIcon() As Boolean
  113.   'Show the icon specified by hIcon in the systray.
  114.   If Not bIconDisplayed Then 'If the icon is not already displayed...
  115.     ShowIcon = Shell_NotifyIcon(NIM_ADD, nidTrayIcon)
  116.     If ShowIcon = False Then 'If there was an error
  117.       RaiseEvent NIError(GetLastError) 'Return the error number
  118.     Else
  119.       bIconDisplayed = True 'The icon is displayed
  120.     End If
  121.   End If
  122. End Function
  123.  
  124. Public Function HideIcon() As Boolean
  125.   'Remove the icon from the systray.
  126.   If bIconDisplayed Then 'If the icon is displayed...
  127.     HideIcon = Shell_NotifyIcon(NIM_DELETE, nidTrayIcon)
  128.     If HideIcon = False Then 'If there was an error
  129.       RaiseEvent NIError(GetLastError) 'Return the error number
  130.     Else
  131.       bIconDisplayed = False 'The icon is not displayed
  132.     End If
  133.   End If
  134. End Function
  135.  
  136. Public Property Let IconHandle(ByVal hIcon As Long)
  137.   'Change the icon displayed in the systray for the icon handled
  138.   'by hIcon. bIconStatus must be true.
  139.   nidTrayIcon.hIcon = hIcon
  140.   If bUpdateOnChange Then
  141.     nidTrayIcon.uFlags = NIF_ICON
  142.     Update 'Make the icon change appear
  143.     nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  144.   End If
  145. End Property
  146.  
  147. Public Property Let TipText(ByVal sTip As String)
  148.   'Change the tooltip text of the icon by sTip
  149.   'Remove the last character of it length is MAX_TIP_LENGTH because
  150.   'we need to include a Null Zero
  151.   nidTrayIcon.szTip = Left(sTip, MAX_TIP_LENGTH - 1) & vbNullChar
  152.   If bUpdateOnChange Then
  153.     nidTrayIcon.uFlags = NIF_TIP
  154.     Update
  155.     nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  156.   End If
  157. End Property
  158.  
  159. Public Property Let CallbackMessage(ByVal uCallbackMessage As Long)
  160.   'Change the callback message that is send to the form.
  161.   'The new message is uCallbackMessage.
  162.   nidTrayIcon.uCallbackMessage = uCallbackMessage
  163.   If bUpdateOnChange Then
  164.     nidTrayIcon.uFlags = NIF_MESSAGE
  165.     Update
  166.     nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  167.   End If
  168. End Property
  169.  
  170. Public Function Update() As Boolean
  171.   'Update the icon on the systray. Call this function when you
  172.   'want to refresh the icon or when you have made a change to
  173.   'a property when bUpdateOnChange is False.
  174.   'Also called internaly when bUpdateOnChange is true.
  175.   If bIconDisplayed Then 'The icon must be showed to make change
  176.     Update = Shell_NotifyIcon(NIM_MODIFY, nidTrayIcon)
  177.     If Update = False Then 'If there was an error
  178.       RaiseEvent NIError(GetLastError) 'Return the error number
  179.     End If
  180.   End If
  181. End Function
  182.  
  183. Public Property Get IconHandle() As Long
  184.   'Return the handle of the current icon
  185.   IconHandle = nidTrayIcon.hIcon
  186. End Property
  187.  
  188. Public Property Get TipText() As String
  189.   'Return the tooltip text w/o the ending null zero
  190.   TipText = Left(nidTrayIcon.szTip, Len(nidTrayIcon.szTip) - 1)
  191. End Property
  192.  
  193. Public Property Get CallbackMessage() As Long
  194.   'Return the current callback message
  195.   CallbackMessage = nidTrayIcon.uCallbackMessage
  196. End Property
  197.  
  198. Public Property Let UpdateOnChange(bUpdate As Boolean)
  199.   'Set the variable bUpdateOnChange to a new value. If bUpdate is
  200.   'True, then